﻿using PBO.Data.Util;
using System;
using System.Data;
using System.Linq;

namespace PBO.Data.SQLite.DAL
{
    public class DALMoveData : DALModel<MoveData>
    {

        #region movedata

        /// <summary>
        /// 摇手指可以选到的技能
        /// </summary>
        /// <param name="moveids">其它不能选到的技能(自身)</param>
        /// <returns></returns>
        public static int[] GetMetronomeMoveIds(params int[] moveids)
        {
            int[] CONNOT_CALL = { 68, 102, 118, 119, 144, 165, 166, 168, 173, 182, 194, 197, 203, 214, 243, 264, 266, 267, 270, 271, 274, 289, 343, 364, 382, 383, 415, 448, 469, 476, 495, 501, 511, 516 };
            string sql = "select moveid from Pbo_Move";
            var dt = DBUtility.ExecuteDataTable(sql);
            var all_moves = from DataRow dr in dt.Rows select Convert.ToInt32(dr[0]);
            return all_moves.Where(id => !CONNOT_CALL.Contains(id) && !moveids.Contains(id)).ToArray();
        }

        /// <summary>
        /// for 0791 team file
        /// </summary>
        public static int GetMoveIdByOldName(string name)
        {
            string sql = "select moveid from Pbo_Move where oldname='" + name + "' limit 1";
            var id = DBUtility.GetSingle(sql);
            return id != null ? Convert.ToInt32(id) : 0;
        }

        //public static int GetMoveByEnName(string name)
        //{
        //    string sql = "select moveid from Pbo_Move where enname like '" + name + "' limit 1";
        //    var id = DbHelperSQLite.GetSingle(sql);
        //    return id != null ? Convert.ToInt32(id) : 0;
        //}

        #endregion

        public string GetMoveInfo(int mid)
        {
            return DBUtility.GetSingle("select info from pbo_move where moveid=" + mid).ToString();
        }

        #region Instance

        private DALMoveData() { }

        private static DALMoveData instance;

        public static DALMoveData GetInstance()
        {
            if (instance == null)
                instance = new DALMoveData();
            return instance;
        }

        #endregion

        #region override

        protected override string GetTableName()
        {
            return "pbo_move";
        }

        protected override MoveData DataRowToModel(DataRow dr)
        {
            var data = new MoveData();

            data.Id = dr["MoveId"].ToString().ToInt();
            data.Name = dr["Name"].ToString();
            data.Accuracy = dr["Accuracy"].ToString().ToInt();
            data.AddEffect1 = (MoveAdditionalEffect)dr["AddEffect1"].ToString().ToInt();
            data.AddEffect2 = (MoveAdditionalEffect)dr["AddEffect2"].ToString().ToInt();
            data.AddEffectOdds = dr["AddEffectOdds"].ToString().ToInt() * 10;
            data.Effect = (MoveEffect)dr["Effect"].ToString().ToInt();
            data.Category = (MoveCategory)dr["Category"].ToString().ToInt();
            data.Power = dr["Power"].ToString().ToInt();
            data.PP = dr["PP"].ToString().ToInt();
            data.Priority = dr["Priority"].ToString().ToInt();
            data.Target = (MoveTarget)dr["TargetId"].ToString().ToInt();
            data.Type = (BattleType)dr["TypeId"].ToString().ToInt();

            string[] arr = { "Contact", "Sound", "KingRock", "Snatchable", "AttackAtTarget", "Substitute", "Protectable", "Punch" };
            for (int i = 0; i < arr.Length; i++)
            {
                data.Details[i] = (bool)dr[arr[i]];
            }

            return data;
        }

        public MoveData GetModel(int id)
        {
            return base.GetModel("moveid=" + id).FirstOrDefault();
        }

        protected override string GetSqlFields()
        {
            const string FIELDS = "MoveId,Name,Accuracy,AddEffect1,AddEffect2,AddEffectOdds,Effect,Category,Power,PP,Priority,TargetId,TypeId,Contact,Sound,KingRock,Snatchable,AttackAtTarget,Substitute,Protectable,Punch";
            return FIELDS;
        }

        #endregion

    }
}
